﻿using System;
using System.Data;
using System.Collections.Generic;
using eTMS.Controllers.CaHocService;
namespace eTMS.Controllers{
public class CaHocControllerBase : CommonController{
	public CaHocControllerBase()
		: base()
	{	
		this.dao = new CaHocServiceClient();
	}
	~CaHocControllerBase(){
        if (dao.State == System.ServiceModel.CommunicationState.Opened) dao.Close();
	}
	private DataTable _Table;
	private CaHocEntity oCaHoc;	
	public CaHocEntity CaHocObject{
		get{
			return oCaHoc;
		}
		set{
			oCaHoc=value;
		}
	}
	protected CaHocServiceClient dao;
	public DataTable Table{
		get{
			this.LoadAllDSCaHoc(false);			
			return _Table;
		}
	}
	public void LoadAllDSCaHoc(bool clearCache){
		bool bForce=clearCache;
		if(_Table==null)
		{
			bForce=true;
		}
		if(!bForce)return;
		DataSet ds=dao.SelectAllDS();
		if(ds.Tables.Count>0){
			_Table=ds.Tables[0];
			this.AddIndexToTable(_Table);
		}
	}	
	public void AddIndexToTable(){
		this.AddIndexToTable(_Table);
	}
	public void ResetTableIndex(){
		this.ResetTableIndex(_Table);
	}
	public bool AddNew(){
		if(oCaHoc==null)return false;
		bool result=true;
		try{
			oCaHoc = dao.SaveRet(oCaHoc);
		}
		catch{
			result=false;
		}
		if(result){
			_ListCaHoc.Add(oCaHoc);
		}
		return result;
	}
	public bool Update(){
		if(oCaHoc==null)return false;
		return dao.Update(oCaHoc);
	}
	public bool Delete(){
		if(oCaHoc==null)return false;
		bool retvalue = dao.Delete(oCaHoc);
		if(retvalue){			
			_ListCaHoc.Remove(oCaHoc);
		}
		return retvalue;
	}
	public bool DeleteByID(Int32 CaHoc_ID){
		bool retvalue = dao.DeleteByID(CaHoc_ID);		
		if(retvalue){
			CaHocEntity oDeleted=FindCaHoc(CaHoc_ID);
			if(oDeleted!=null){
				_ListCaHoc.Remove(oDeleted);
			}
		}
		return retvalue;
	}
	public void ResetObject(){
		this.oCaHoc = new CaHocEntity();
	}
	//Check if exists MaCa
	public bool CheckExistsMaCa(object value){	
		return dao.SelectOneByMaCa(value.ToString()).MaCa!=null;
    }    
    //Copy data from data row in grid to object
    public CaHocEntity MapDataRowToObject(DataRow row){
		CaHocEntity o = new CaHocEntity();
		 o.MaCa=Convert.ToString(row["MaCa"]);
		 o.TenCa=Convert.ToString(row["TenCa"]);
		 o.GhiChu=Convert.ToString(row["GhiChu"]);
		 if(!row.IsNull("CaHoc_ID"))
		    o.CaHoc_ID=(Int32)(row["CaHoc_ID"]);
		 return o;
    }
	//Copy data from object to data row in grid    
    public void MapObjectToDataRow(CaHocEntity o,DataRow row){
		row["MaCa"]=o.MaCa;
		row["TenCa"]=o.TenCa;
		row["GhiChu"]=o.GhiChu;
		row["CaHoc_ID"]=o.CaHoc_ID;
    }
    public void MapObjectToDataRow(DataRow row){
		this.MapObjectToDataRow(this.CaHocObject,row);
    }
	public void LoadObject(Int32 CaHoc_ID){
		CaHocEntity oLoad=FindCaHoc(CaHoc_ID);
		if(oLoad==null){
			oCaHoc=this.LoadCaHoc(CaHoc_ID);		
			bool bNotIsNull= true;
			 bNotIsNull&=oCaHoc.IsNullCaHoc_ID;
			if(!bNotIsNull){
				this.ListCaHoc.Add(oCaHoc);
			}
		}
		else{
			oCaHoc=oLoad;
		}
	}
	#region CachedList CaHoc
	private static List<CaHocEntity> _ListCaHoc=new List<CaHocEntity>();
    public List<CaHocEntity>ListCaHoc{
		get{
			return _ListCaHoc;
		}
	}
	public static List<CaHocEntity>ListCaHocStatic{
		get{
			return _ListCaHoc;
		}
	}
	public CaHocEntity FindCaHoc(Int32 CaHoc_ID){
		CaHocEntity oResult=this.ListCaHoc.Find(delegate(CaHocEntity oFind){
			bool result=true; 
			result&=oFind.CaHoc_ID.Equals(CaHoc_ID);
			return result;
		 });
		return oResult;
	}
	public static void LoadAllCaHoc(bool clearCache){
		if(_ListCaHoc.Count==0)
			clearCache=true;
		if(!clearCache)return;		
		CaHocServiceClient dao = new CaHocServiceClient();
		_ListCaHoc=new List<CaHocEntity>(dao.SelectAll());
		dao.Close();
	}
	public CaHocEntity LoadCaHoc(Int32 CaHoc_ID){			
		CaHocEntity o=dao.SelectOne(CaHoc_ID);		
		return o;
	}
	#endregion
}
}
